package com.servlet.ihm;

import java.io.IOException;
import java.util.ArrayList;
import java.util.TreeSet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.listener.ContextListener;
import com.servlet.model.base.News;
import com.servlet.model.base.Profile;

public class NewsServlet extends HttpServlet
{ 
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{
		HttpSession session = request.getSession();
		
		String email = (String)session.getAttribute("email");

		Profile profile = ContextListener.base.findProfile(email);
		
		request.setAttribute("profile", profile );

		request.setAttribute("email", email );
		
		ArrayList<News> news = ContextListener.base.findNews(email);
		
		for(int i = 0 ; i < news.size() ; i++ )
		{
			news.get(i).setProfile1( ContextListener.base.findProfile(news.get(i).getEmail_user1()) );
			news.get(i).setProfile2( ContextListener.base.findProfile(news.get(i).getEmail_user2()) );
			
			if( news.get(i).getGenre() == 3 )
				news.get(i).setProfile3( ContextListener.base.findProfile(news.get(i).getInfo_sup()) );
		}
		
		TreeSet<News> tnews = new TreeSet<News>( news );

		request.setAttribute("news", tnews );
		
		//quelle rubrique est actuellement en surbrillance
		request.setAttribute("page", new String("news"));
		//rubrique dans la page
		request.setAttribute("rubrique", new String("Main"));
		RequestDispatcher dispatch = request
				.getRequestDispatcher("jsp/pageBase.jsp");
		dispatch.forward(request, response);
	}
}
